package com.paireach.szwolaiyun.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.paireach.szwolaiyun.R;
import com.paireach.szwolaiyun.mode.SignMode;
import com.paireach.szwolaiyun.mode.SignResultMode;
import com.paireach.szwolaiyun.sz_common_widget.fragment.BaseFragment;
import com.paireach.szwolaiyun.sz_util.SharedPreferencesUtil;
import com.paireach.szwolaiyun.util.BaiDuSimpleLocationHelp;
import com.paireach.szwolaiyun.util.MessageInfoUtil;
import com.paireach.szwolaiyun.util.StringUtil;

import szwolaiyun.paireach.com.szwolaizhuang.model.event.AppEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpErrorEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpSuccessEvent;
import szwolaiyun.paireach.com.szwolaizhuang.net.WayBillRequest;

/**
 * Created by Administrator on 2016/6/21.
 */
public class SignFragment extends BaseFragment implements View.OnClickListener {

    public static final String TAG = "SignFragment";
    private View view;
    private Handler handler;
    private Button sign;//到达送货工厂签到
    private Button sign2;//到达收货工厂签到

    private RelativeLayout sgin_start_point;//到达送货工厂签到时显示的layout

    private RelativeLayout sgin_end_point;//到达收货工厂签到时显示的layout

    private TextView address;
    private TextView space;
    private TextView time_appointment;

    private SignMode signMode;

    private SignResultMode signResultMode;

    private String orderNo;

    //定位帮助类
    private BaiDuSimpleLocationHelp baiDuSimpleLocationHelp;

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
    }

    public static final SignFragment getInstance(String orderNo) {
        SignFragment fragment = new SignFragment();
        Bundle bundle=new Bundle();
        bundle.putString("orderNo",orderNo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle=getArguments();
        if(bundle!=null){
            orderNo=bundle.getString("orderNo");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_sign, null);
        baiDuSimpleLocationHelp = new BaiDuSimpleLocationHelp();
        initControl();
        init();
        return view;
    }

    public void initHandle(Handler handler) {
        this.handler = handler;
    }

    private void initControl() {
        sign = (Button) view.findViewById(R.id.sign);
        sign2 = (Button) view.findViewById(R.id.sign2);
        sgin_start_point = (RelativeLayout) view.findViewById(R.id.sgin_start_point);
        sgin_end_point = (RelativeLayout) view.findViewById(R.id.sgin_end_point);
        address = (TextView) view.findViewById(R.id.address);
        space = (TextView) view.findViewById(R.id.space);
        time_appointment = (TextView) view.findViewById(R.id.time_appointment);
        sign.setOnClickListener(this);
        sign2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sign:
                baiDuSimpleLocationHelp.startGetLocation(getActivity(), httpAndLocationHandle, null);
                break;
            case R.id.sign2:
                //调用定位，上传位置坐标，得到返回结果
                baiDuSimpleLocationHelp.startGetLocation(getActivity(), httpAndLocationHandle, null);
                break;
        }
    }

    private void init() {
        if (signMode.getStatusCode().equals(StringUtil.GO_TO_SHIPMENT)) {
            //装货在途显示layout1
            try {
                sgin_start_point.setVisibility(View.VISIBLE);
                sgin_end_point.setVisibility(View.GONE);
                address.setText("装货工厂地址:  " + signMode.getAddress());
                time_appointment.setText("预约时间 ： " + signMode.getDate());
            } catch (Exception e) {
                Log.e("0011", "出现异常", e);
            }


        } else {
            //送货在途显示layout2
            sgin_start_point.setVisibility(View.GONE);
            sgin_end_point.setVisibility(View.VISIBLE);
        }
    }

    private Handler httpAndLocationHandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case BaiDuSimpleLocationHelp.getLocationSucceed:
                    BDLocation location = (BDLocation) msg.obj;
//                    Log.e("0011","签到页面定位信息"+location.getAddrStr());
//                    if(signMode.getStatusCode().equals(StringUtil.GO_TO_SHIPMENT)){
//                        //到达装货工厂签到
//                        MyHttpUtil.sign("801",signMode.getCode(),location.getLatitude()+"",location.getLongitude()+"",signMode.getWarehouseType(),signMode.getScSupportBking(),httpAndLocationHandle);
//
//                    }else if(signMode.getStatusCode().equals(StringUtil.IN_DELIVERY)){
//                        //到达收货工厂签到
//                        MyHttpUtil.sign("803",signMode.getCode(),location.getLatitude()+"",location.getLongitude()+"",signMode.getWarehouseType(),signMode.getScSupportBking(),httpAndLocationHandle);
//                    }

                    new WayBillRequest().saveOrderRunningTrack(orderNo,
                            SharedPreferencesUtil.getValue(getActivity(), SharedPreferencesUtil.HOTLINE),
                            location.getLongitude(),
                            location.getLatitude(),
                            String.valueOf(location.getSpeed()),
                            String.valueOf(location.getDirection()),
                            String.valueOf(location.getAddress()),
                            location.getProvince(),
                            location.getCity(),
                            location.getDistrict(), null, null);
                    break;
                case MessageInfoUtil.SIGN_SUCCEED:
                    Log.e("0011", "签到成功信息为" + msg.obj);
                    signResultMode = new SignResultMode();
                    signResultMode.init(msg.obj.toString());
                    if (signResultMode.getResultFlag()) {
                        //进入扫码页面
                    } else {
                        Toast.makeText(getActivity(), signResultMode.getMessage(), Toast.LENGTH_LONG).show();
                    }
                    break;
                case MessageInfoUtil.SIGN_FAIL:
                    Log.e("0011", "签到失败信息为" + msg.obj);
                    break;
            }
        }
    };
}
